import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/Login.vue'
import Home from '@/views/Home.vue'
import NewsPool from '@/views/NewsPool.vue'
import PressRelease from '@/views/PressRelease.vue'
import NewsHasBeenReleased from '@/views/NewsHasBeenReleased.vue'


const routes = [
  {
    path: '/',
    name:'Home',
    component: Home,
    redirect: '/NewsPool',
    children: [
      {
        path: 'NewsPool',
        name:'NewsPool',
        component: NewsPool,
      },
      {
        path: 'PressRelease',
        name:'PressRelease',
        component: PressRelease,
      },
      {
        path: 'NewsHasBeenReleased',
        name:'NewsHasBeenReleased',
        component: NewsHasBeenReleased,
      },
      {
        path: 'Internationalization',
        name:'Internationalization',
        component: ()=>import('@/views/InternationAlization.vue'),
      },
    ],
  },
  {
    path: '/Login',
    name:'Login',
    component: Login,
  },
  {
    path: '/preview',
    name:'preview',
    component: ()=>import('@/page/preview.vue'),
  },
]


const router = createRouter({
  history: createWebHistory(),
  routes,
})

router.beforeEach((to,from)=>{
  const authenticated:()=>boolean = ()=>{
    const loginInfo = localStorage.getItem('login')
    return !!loginInfo
  }
  if(to.name !=='Login' && !authenticated()){
    return {name:'Login'}
  }

  return true
})

export default router
